package pkg1;
//windowListener implements
//some Events actioned by control
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;

import javax.swing.JFrame;

public class Demo3 extends JFrame{
	private static final long serialVersionUID = 1L;//version logo
	public static void main(String[] args) {
		Demo3 frame1 = new Demo3();
		frame1.setVisible(true);
	}
	public Demo3() {
		super();
		//add focusListener
		addWindowFocusListener(new MyWindowFocusListener());
		addWindowStateListener(new MyWindowStateListener());
		addWindowListener(new MyWindowListener());
		
		setTitle("Catch window focus Event");
		setBounds(100, 100, 500, 375);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	private class MyWindowFocusListener implements WindowFocusListener {
		@Override
		public void windowGainedFocus(WindowEvent e) {
			System.out.println("the window is on focus!");
		}
		@Override
		public void windowLostFocus(WindowEvent e) {
			System.out.println("the window lost focus!");
		}
	}
	private class MyWindowStateListener implements WindowStateListener {
		public void windowStateChanged(WindowEvent e) {
			int oldState = e.getOldState();//get the window's old state
			int newState = e.getNewState();//get new window state
			String from = "";//flag string before
			String to = ""; 	//flag string now
			switch(oldState) {//judge before
			case Frame.NORMAL:
				//window is normal
				from = "Normal";
				break;
			case Frame.MAXIMIZED_BOTH:
				//window is maxSize
				from = "Maxmized";
				break;
			default:
				//window is minSize
				from = "Minized";
			}
			switch(newState) {//judge nowstate
			case Frame.NORMAL:
				to = "Normal";
				break;
			case Frame.MAXIMIZED_BOTH:
				to = "Maxmized";
				break;
			default:
				to = "Minized";
			}
			System.out.println(from + "-->" + to);//fomat print
		}
	}
	
	private class MyWindowListener implements WindowListener {
		public void windowOpened(WindowEvent e) {
			System.out.println("window opened!");
		}

		public void windowClosing(WindowEvent e) {
			System.out.println("window closing!");
		}

		@Override
		public void windowClosed(WindowEvent e) {
			System.out.println("window closed!");
		}

		public void windowIconified(WindowEvent e) {
			System.out.println("window inconified!");
		}

		public void windowDeiconified(WindowEvent e) {
			System.out.println("window deiconified!");
		}

		public void windowActivated(WindowEvent e) {
			System.out.println("window activated!");
		}

		public void windowDeactivated(WindowEvent e) {
			System.out.println("window deactivated");
		}
	}
}
